package com.azt.novel.news.service.impl;


import com.azt.novel.common.resp.RestResp;
import com.azt.novel.news.dao.entity.NewsContent;
import com.azt.novel.news.dao.entity.NewsInfo;
import com.azt.novel.news.dao.mapper.NewsContentMapper;
import com.azt.novel.news.dao.mapper.NewsInfoMapper;
import com.azt.novel.news.dto.resp.NewsInfoRespDto;
import com.azt.novel.news.manager.cache.NewsCacheManager;
import com.azt.novel.news.service.INewsInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 新闻信息 服务实现类
 * </p>
 *
 * @author azt
 * @since 2023-12-02
 */
@Service
public class NewsInfoServiceImpl implements INewsInfoService {

    @Resource
    private NewsCacheManager newsCacheManager;

    @Resource
    private NewsInfoMapper newsInfoMapper;

    @Resource
    private NewsContentMapper newsContentMapper;

    @Override
    public RestResp<NewsInfoRespDto> getNewsById(Long newsId) {
        NewsInfo newsInfo = newsInfoMapper.selectById(newsId);
        LambdaQueryWrapper<NewsContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NewsContent::getNewsId,newsId)
                .last("limit 1");
        NewsContent newsContent = newsContentMapper.selectOne(queryWrapper);
        return RestResp.ok(NewsInfoRespDto.builder()
                .title(newsInfo.getTitle())
                .sourceName(newsInfo.getSourceName())
                .updateTime(newsInfo.getUpdateTime())
                .content(newsContent.getContent())
                .build());
    }

    @Override
    public RestResp<List<NewsInfoRespDto>> listLatestNews() {
        return RestResp.ok(newsCacheManager.listLatestNews());
    }
}
